package store.lunangangster.nodeflow.parser.el;

import store.lunangangster.nodeflow.exception.RuleSourceParseException;
import store.lunangangster.nodeflow.rt.element.flow.IfFlow;

/**
 * 用于处理ELOperator的工具类
 *
 * @author cwk
 * @version 1.0
 */
public class OperatorHelper {
  public static void checkArguments(Object[] arguments) throws RuleSourceParseException{
    if (arguments != null) {
      for (Object o : arguments) {
        if (o == null) {
          throw new RuleSourceParseException("arguments cannot be null");
        }
      }
    }
  }

  /**
   * IfFlow是一个二叉数型结构，该方法会从树根开始向falseBranch方向一直遍历到叶子结点。
   * 如果IfFlow还没有ELSE分支，则最后遍历到到falseBranch一定是IfFlow
   * @param rootFlow  IfFlow树根
   * @return          最底层到IfFlow，一般是树根或者是ELSEIF产生的IfFlow
   */
  public static IfFlow findFalseBranchLeaf(IfFlow rootFlow) {
    if (rootFlow == null || rootFlow.isElse()) {
      return null;
    }
    IfFlow currFlow = rootFlow;
    while (currFlow.getFalseBranch() != null) {
      // no check
      currFlow = (IfFlow) currFlow.getFalseBranch();
    }
    return currFlow;
  }

}
